package com.example.service;

import com.example.Random;
import com.example.mapper.CourseMapper;
import com.example.model.Course;
import com.example.model.TeacherCourse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CourseService {
    private final CourseMapper createCourseMapper;
    @Autowired
    public CourseService(CourseMapper createCourseMapper){
        this.createCourseMapper=createCourseMapper;
    }
    public void createCourse(Course course){
        course.setCourseId(Random.random());
        createCourseMapper.createCourse(course);
    }
    public void deleteCourse(String courseId){
        createCourseMapper.deleteCourse(courseId);
    }
    public void updateCourse(Course course){
        createCourseMapper.updateCourse(course);
    }
    public String joinCourse(TeacherCourse teacherCourse){
        if(isCourseExit(teacherCourse.getCourseId())){
            if(isMyCourse(teacherCourse.getCourseId(),teacherCourse.getTid())){
                if(isIntoCourse(teacherCourse.getTid(),teacherCourse.getCourseId())){
                    createCourseMapper.joinCourse(teacherCourse);
                    return "success";
                }else {
                    return "already exit";
                }
            }else {
                return "your course";
            }
        }else {
            return "not exit";
        }
    }
    Boolean isCourseExit(String courseId){
       int count = createCourseMapper.courseCount(courseId);
        return count == 1;
    }
    Boolean isMyCourse(String courseId,String Tid){
       int count = createCourseMapper.isMyCourse(courseId, Tid);
        return count == 0;
    }
    Boolean isIntoCourse(String Tid,String courseId){
        String InputCourseId = createCourseMapper.isInCourse(Tid);
        return !courseId.equals(InputCourseId);
    }
  public List<Course> select(String number){
       return createCourseMapper.allCourse(number);
   }
   public List<Course> selectAllCourse(String Tid){
        return createCourseMapper.createAllCourse(Tid);
   }

}
